import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $Lazy,$http } = window
// ===================================================================== Lazy
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		//修改账期
		formData:[
			{ label: '账期',			name: 'summary_sn', type: 'select', 	full:true,	loading:true, required:true, 
				onValue:({value,formData})=>{
					const {hosp_id} = this.formRef.getValue(['hosp_id'])
					if(value&&hosp_id)this.getBill({hosp_id,summary_sn:value})
				} ,
			},
			{ label: '受检单位',		name: 'hosp_id', 	type: 'select', 	half:true,	loading:true, nameStr:'name', required:true,
				onValue:({value,formData})=>{
					const {summary_sn} = this.formRef.getValue(['summary_sn'])
					if(value&&summary_sn)this.getBill({summary_sn,hosp_id:value})
				}
			 },
			{ label: '开票单位',		name: 'organization_name', 	half:true, required:true },
			{ label: '暂估金额',		name: 'estimate_price', 	half:true, required:true },
			{ label: '业务员',		name: 'salesman_id', 		type: 'select', 	half:true, 
				required:true, nameStr:'real_name', idStr:'salesman_id'
			},
			{ label: '区域',		name: 'region_name', 	half:true, required:true, disabled:true },
		]
	}
	
	model = {}
	
	open = (data) => {
		this.modal.open({ formRef:this.formRef })
		this.getSelect(data)
	}

	// 获取下拉数据
	getSelect = (data) => {
		const { formData } = this.state
		$cache.then( async f => {
			// 账期
			$fn.setRow(formData, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) ) 
			// // 业务员
			// $fn.setRow(formData, 'salesman_id', await f.getCache({ api:'hospital' } ) )
			// 医院名称
			$fn.setRow(formData, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			this.setState({ formData })
		})
	}
	
	getBill = param => {
		const { formData } = this.state
		$http.submit(null,'fi-standing-book/getHospSummaryPrice',{param:{...param, type:1}}).then(data=>{
			const salemanData = data.salesman
			$fn.setRow(formData, 'salesman_id', salemanData )
			this.setState({ formData })
			this.formRef.setValue({
				estimate_price:data.summary_sn.estimate_price,
				organization_name:data.hosp.hosp_name,
				region_name:data.hosp.region_name
			})
		})
	}
	
	render(){
		return FormModal.call(this,{ title:'填写台账', api: 'fi-standing-book/save'})
	}
}

export default Index